package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.WorkItemBean;
import com.yanchangapp.model.request.MakeDailyPlanRequest;
import com.yanchangapp.model.request.WorksRequest;
import com.yanchangapp.ui.view.WorkItemAtView;
import com.zxl.baselib.bean.response.BaseListResponse;
import com.zxl.baselib.bean.response.BasePageListResponse;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.zxl.baselib.util.ui.UIUtils;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-10-10.
 * description:我的工作项P层
 */
public class WorkItemAtPresenter extends BasePresenter<WorkItemAtView> {
    public WorkItemAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryWorkItemLists(int curPage){
        HeadApiRetrofit.getInstance().postQueryWorkItemList(new WorksRequest(curPage))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BasePageListResponse<WorkItemBean>>() {
                    @Override
                    public void onNext(BasePageListResponse<WorkItemBean> workItemBeanBasePageListResponse) {
                        if (getView() != null){
                            if (workItemBeanBasePageListResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                                getView().queryWorksSuccess(workItemBeanBasePageListResponse.getData().getList());
                            }else {
                                getView().queryWorksFailure(workItemBeanBasePageListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().queryWorksFailure("网络错误");
                        }
                    }
                });

    }

    public void makeDailyPlan(List<MakeDailyPlanRequest> list){
        getView().showLoadingDialog();
        HeadApiRetrofit.getInstance().postMakeDailyPlan(list)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                                getView().makePlanSuccess("生成今日计划成功");
                            }else {
                                getView().makePlanFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            getView().makePlanFailure("网络错误");
                        }
                    }
                });
    }
}
